<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action=""
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="uploadPdf"
    >
      <img v-if="pdfUrl" :src="pdfUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>

import axios from "axios";
const URL = process.env.VUE_APP_BASE_API
const defaultUrl = URL + '/upload/file';
export default {
  props: {
    value: {
      type: String
    }
  },
  data() {
    return {
      uploadUrl: '',
      pdfUrl: ''
    }
  },

  methods: {
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 100;
      return isLt2M;
    },
    uploadPdf(params) {
      const form = new FormData();
      form.append('file', params.file);
      const uploadUrl = defaultUrl
      axios.post(uploadUrl, form, {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
      }).then(response => {
        if (response.data.code == 200) {
          this.pdfUrl = response.data.data.url
          this.$emit('input', this.pdfUrl)
        }
        this.$message.success(response.data.msg);
      }).catch(error => {
        console.log(error);
        this.$message.error("上传失败:" + error);
      })
    },
  },
  mounted() {
    this.pdfUrl = this.value
  }
}
</script>

<style scoped lang="scss">
.avatar-uploader ::v-deep.el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader ::v-deep.el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
